import router from '../router/index';
// import Pinyin from 'pinyin';

const gethidePhone = (phone: string): string => {
  let number = phone; //获取到手机号码字段
  // console.log('手机号', this.tel)
  let mphone = number.substring(0, 3) + '****' + number.substring(7);
  return mphone;
};

// http://101.34.143.36:8890/api/image
const imgLinkUrl = (url: string): string => {
  return 'http://101.34.143.36:8890/api/image/' + url;
};
const filterHTML = (input: string) => {
  return input.replace(/(<([^>]+)>)/gi, '');
};

const handlemouse = (e: string) => {
  const nav = document.getElementById(e);
  var flag;
  var downX; // 鼠标点击的x下标
  var scrollLeft; // 当前元素滚动条的偏移量
  nav.addEventListener('mousedown', function (event) {
    flag = true;
    downX = event.clientX; // 获取到点击的x下标
    scrollLeft = this.scrollLeft; // 获取当前元素滚动条的偏移量
    event.stopPropagation();
  });
  nav.addEventListener('mousemove', function (event) {
    if (flag) {
      // 判断是否是鼠标按下滚动元素区域
      var moveX = event.clientX; // 获取移动的x轴
      var scrollX = moveX - downX; // 当前移动的x轴下标减去刚点击下去的x轴下标得到鼠标滑动距离
      this.scrollLeft = scrollLeft - scrollX; // 鼠标按下的滚动条偏移量减去当前鼠标的滑动距离
      // console.log(scrollX);
      event.stopPropagation();
    }
  });

  // const handler = (event: any) => {
  //   // 获取滚动方向
  //   const detail = event.wheelDelta || event.detail;
  //   // 定义滚动方向，其实也可以在赋值的时候写
  //   const moveForwardStep = 1;
  //   const moveBackStep = -1;
  //   // 定义滚动距离
  //   let step = 0;
  //   // 判断滚动方向,这里的100可以改，代表滚动幅度，也就是说滚动幅度是自定义的
  //   if (detail < 0) {
  //     step = moveForwardStep * 100;
  //   } else {
  //     step = moveBackStep * 100;
  //   }
  //   // 对需要滚动的元素进行滚动操作
  //   nav.scrollLeft += step;
  //   event.stopPropagation();
  // };
  // nav.addEventListener('mousewheel', handler, false);

  // 鼠标抬起停止拖动
  nav.addEventListener('mouseup', function (event) {
    flag = false;
    event.stopPropagation();
  });
  // 鼠标离开元素停止拖动
  nav.addEventListener('mouseleave', function (event) {
    flag = false;
    event.stopPropagation();
  }); 
};

const pathSquare = (e: number) => {
  // router.push('/template/templateMould/templateItem2');
  let url: string = '';
  switch (e) {
    case 395740859879493:
      url = 'templateMould/templateItem1/templateHome';
      return url;
      break;
    case 395741054230597:
      url = 'templateItem2/home';
      return url;
      break;
    case 410276243316805:
      url = 'levelling/home';
      return url;
      break;
    case 410276336631877:
      url = 'calligraphy';
      return url;
      break;
    case 420838252666949: //模板五
      url = 'timeAxis/timeAxisHome';
      return url;
      break;
    case 420838403928133: //模板6
      url = 'templateItem6/home';
      return url;
      break;
    case 420838478893125: //模板7
      url = 'Biography/home/homeInfo';
      return url;
      break;
    default:
      break;
  }
};
const getTextHtml = (e: string | null): any => {
  //获取文本域转换成标签
  if (!e) {
    return;
  }
  let arr = e.split('\n');
  let text = '';
  arr.forEach((el) => {
    if (el) {
      text = text + `<span>${el}</span>`;
    }
  });
  return text;
  // router.push({ path: '/login' });
};
const convertToPinyin = (text: string) => {
  return '';
  // if (text) {
  //   let str = Pinyin(text, {
  //     style: Pinyin.STYLE_NORMAL,
  //   }).join(' ');
  //   return str; 
  // } else {
  //   return ''; 
  // }
};
const pagerCount = (count: number, pageSize: number) => {
  if (typeof count == 'number') {
    if (count > 0) {
      try {
        let _pagerCount: number = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        let c = Number(_pagerCount.toFixed(0)); //小数取整
        _pagerCount = c > _pagerCount ? c - 1 : c; //过滤四舍五入
        return Number(_pagerCount);
      } catch (error) {
        return 0;
      } 
    } else { 
      return 0; 
    }
  } else {
    return 0;
  }
}; 
let cancatUrl: string = 'http://t-www.jiyi.show/ex/#/';
let linkurl: string = 'http://t-www.jiyi.show'; 
let workbenchUrl: string = 'http://t-workbench.jiyi.show/#/'; 
let doorUrl: string = 'http://t-www.jiyi.show/';  
if (import.meta.env.MODE !== 'development') {   
  cancatUrl = '//www.jiyi.show/ex/#/';
  linkurl = '//www.jiyi.show';
  workbenchUrl = '//workbench.jiyi.show/#/';
  doorUrl = '//www.jiyi.show/';
}
const publicUrls: string = 'https://static.jiyi.show/';
export { 
  doorUrl,
  workbenchUrl, 
  linkurl, 
  cancatUrl,
  publicUrls, 
  pagerCount,
  getTextHtml,  
  pathSquare,
  gethidePhone,
  imgLinkUrl,
  handlemouse,
  convertToPinyin,
  filterHTML,
};
